package com.yzg.simple.netty.dubbo.netty;

import com.yzg.simple.netty.dubbo.specification.TransferMethod;
import com.yzg.simple.netty.dubbo.specification.TransferMethodImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author misterWei
 * @create 2020年01月09号:23点54分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 做处理的服务端Handler 并调用本地API方法
 */
public class NettyRpcServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      /* ctx.close();
       cause.printStackTrace();*/
    }

    //处理消息 调用API
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //判断当前消息是否满足
        if (msg.toString().startsWith(TransferMethod.LIMIT)){
            //如果满足调用远程方法栈
            String mes =  new TransferMethodImpl().invokerMethod(msg.toString().substring(msg.toString().lastIndexOf("#")+1));
            ctx.writeAndFlush(mes);
        }
    }
}
